﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using VideoClubGorbea.Common.Interfaces;
using VideoClubGorbea.Infraestructure;
using VideoClubGorbea.ServicesClassLibrary;
using VideoClubGorbea.ServicesClient;
using VideoClubGorbea.ServicesClient.Interfaces;
using VideoClubGorbea.ServicesClient.Proxy;

namespace VideoClubGorbea.Common.Factory
{

    static public class MovieServicesFactory
    {
        public static IUnityContainer Container { get; set; }

        static MovieServicesFactory()
        {
            Database.SetInitializer<VideoClubGorbeaContext>(new DummyDataInitializer<VideoClubGorbeaContext>());     
            CreateInyections();
        }

        private static void CreateInyections()
        {
            Container = new UnityContainer();
           
            Container
                .RegisterType<IVIdeoClubGorbeaContext, VideoClubGorbeaContextMock>()
                .RegisterType<VideoClubGorbea.Common.Interfaces.IMovieServices, MovieServices>();
            DecideDistributedImplementation();
            Container.RegisterType<IMovieServicesProxy, MovieServicesProxy>();
        }

        private static void DecideDistributedImplementation()
        {
            bool isDistributed = false;
            var str = ConfigurationManager.AppSettings["DistributedMode"];
            Boolean.TryParse(str, out isDistributed);

            if (isDistributed)
            {
                Container.RegisterType<IMovieServicesClient, MovieServicesClient>();
            }
            else
            {
                Container.RegisterType<IMovieServicesClient, CustomMovieServicesCient>();
            }
        }

        public static IMovieServicesProxy GetMovieServicesProxy()
        {       
            var result = Container.Resolve<IMovieServicesProxy>();
            return result;
        }

    
    }
}
